﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WechatModule.API;

using System.Data.SqlClient;
using WanCommon.Entity;
using WanCommon.LogModule;
using RgUtility.Common;

namespace WanWebApplication.Wechat
{
    public partial class GetUserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region
                var idStr = Request.QueryString["state"];
                string code = Request.QueryString["code"];
                DateTime startTime = DateTime.Now;
                Log log0 = new Log();

                log0.OperateUserId = "";
                log0.OperateUserType = LogBll.UnAuthWeb;
                log0.Content = string.Format("用户同意获取code{{ code:{0}" +
                    "}}", code + ""
                    );
                #region 日志
                LogBll.Write(log0);
                if (string.IsNullOrWhiteSpace(code))
                {
                    Response.Redirect("./ErrorInfo.aspx?info=" + HttpUtility.UrlEncode("获取code失败！"));
                    return;
                }
                #endregion

                var br = WechatOAuthAPI.GetOAuthToken(code);
                #region 日志

                Log log = new Log();
                if (br.ResultState == BizState.Ok)
                {

                    log.OperateUserId = br.Data.access_token;
                    log.OperateUserType = LogBll.UnAuthWeb;
                    log.Content = string.Format("获取登录授权access_token{{ access_token:{0}," +
                        "openid：{1} }}",
                        br.Data.access_token, br.Data.openid
                        );
                }
                else
                {
                    log.OperateUserId = "";
                    log.OperateUserType = LogBll.UnAuthWeb;
                    log.Content = string.Format("获取登录授权access_token{{ errcode:{0}," +
                        "errmsg：{1} }}",
                        br.Data1.errcode, br.Data1.errmsg
                        );
                }
                LogBll.Write(log);
                if (br.ResultState != BizState.Ok)
                {
                    Response.Redirect("./ErrorInfo.aspx?info=" + HttpUtility.UrlEncode(br.Message));
                    return;
                }
                #endregion
                var br1 = WechatBaseAPI.GetUserInfo(br.Data.openid);
                #region 日志
                var log1 = new Log();
                if (br.ResultState == BizState.Ok)
                {
                    log1.OperateUserId = br1.Data.openid;
                    log1.OperateUserType = LogBll.AuthWechatWeb;
                    var sex = "未知";
                    if (br1.Data.sex == "1")
                    {
                        sex = "男性";
                    }
                    if (br1.Data.sex == "0")
                    {
                        sex = "女性";
                    }
                    log1.Content = string.Format("获取用户信息(耗时:" + (DateTime.Now - startTime).TotalMilliseconds + "毫秒){{ openid:{0},昵称：{1}," +
                        "性别：{2},地址:{3} }}",
                        br1.Data.openid, br1.Data.nickname, sex,
                        (br1.Data.province + br1.Data.city)
                        );
                }
                else
                {
                    log1.OperateUserId = "";
                    log1.OperateUserType = LogBll.AuthWechatWeb;
                    log1.Content = string.Format("获取用户信息(耗时:" + (DateTime.Now - startTime).TotalMilliseconds + "毫秒){{ errcode:{0}," +
                        "errmsg：{1} }}",
                        br1.Data1.errcode, br1.Data1.errmsg
                        );
                }

                LogBll.Write(log1);
                #endregion
                if (br.ResultState != BizState.Ok)
                {
                    Response.Redirect("./ErrorInfo.aspx?info=" + HttpUtility.UrlEncode(br.Message));
                    return;
                }
                Global.WechatDict[br1.Data.openid] = br1.Data;
                //
                Response.Redirect("../RG.aspx?Id=" + HttpUtility.UrlEncode(idStr) + "&openid=" + br1.Data.openid + "&isCache=0");
                #endregion
            }
            catch (ArgumentNullException ex) //通常wechat的强制断开连接是AgreumentNullExcption
            {
                Response.Redirect("./ErrorInfo.aspx?info=" + HttpUtility.UrlEncode("授权服务器连接中断！"));
            }
        }
    }
}